package com.kd.crawler.dao;

import org.apache.commons.lang3.StringUtils;

import com.kd.crawler.entity.CrawlerEntry;
import com.kd.crawler.entity.Document;
import com.kd.utils.DateUtils;

public class StatisticDocumentDao extends BaseDao {

	private static final StatisticDocumentDao instance = new StatisticDocumentDao();
	
	private StatisticDocumentDao() {
		
	}
	
	public static StatisticDocumentDao getInstance() {
		return instance;
	}
	
	public int insert(CrawlerEntry ce, Document doc, String analyzerFlag) {
		String sql = "insert into tb_statistic_document(job_id, entry_id, url, title, date, author, content, analyzer_flag) values(?, ?, ?, ?, ?, ?, ?, ?)";
		String author = doc.getAuthor();
		if(StringUtils.isNotBlank(author)&&100<author.length()) {
			author = author.substring(0, 100);
		}
		Long date = doc.getDate();
		try {
			return this.update(sql, ce.getJobID(), ce.getEntryId(), ce.getUrl(), doc.getTitle(), null==date?null:DateUtils.format(date, "yyyy-MM-dd HH:mm:ss"), author, doc.getContent(), analyzerFlag);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
}
